package com.innovations.inn.service.impl;

import com.innovations.common.core.domain.entity.SysRole;
import com.innovations.common.utils.DateUtils;
import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.BusinessIndividual;
import com.innovations.inn.domain.response.ResponseBusinessIndividualInfo;
import com.innovations.inn.mapper.BusinessIndividualMapper;
import com.innovations.inn.service.IBusinessIndividualService;
import com.innovations.system.service.ISysRoleService;
import com.innovations.system.service.ISysUserService;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商业用户信息Service业务层处理
 *
 * @author dp
 * @date 2024-08-17
 */
@Service
public class BusinessIndividualServiceImpl implements IBusinessIndividualService {
    @Autowired
    BusinessIndividualMapper businessIndividualMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询商业用户信息
     *
     * @param businessIndividualId 商业用户信息主键
     * @return 商业用户信息
     */
    @Override
    public BusinessIndividual selectBusinessIndividualByBusinessIndividualId(Long businessIndividualId) {
        return businessIndividualMapper.selectBusinessIndividualByBusinessIndividualId(businessIndividualId);
    }

    /**
     * 查询商业用户信息列表
     *
     * @param businessIndividual 商业用户信息
     * @return 商业用户信息
     */
    @Override
    public List<BusinessIndividual> selectBusinessIndividualList(BusinessIndividual businessIndividual) {
        return businessIndividualMapper.selectBusinessIndividualList(businessIndividual);
    }

    /**
     * 查询商业用户详细信息列表
     *
     * @param businessIndividual 商业用户信息
     * @return 结果
     */
    @Override
    public List<ResponseBusinessIndividualInfo> selectBusinessIndividualListInfo(BusinessIndividual businessIndividual) {
        return businessIndividualMapper.selectBusinessIndividualListInfo(businessIndividual);
    }

    /**
     * 新增商业用户信息
     *
     * @param businessIndividual 商业用户信息
     * @return 结果
     */
    @Override
    public int insertBusinessIndividual(BusinessIndividual businessIndividual) {
        // 状态为审核通过时，添加商业角色
        if (businessIndividual.getState() == 2) {
            List<SysRole> roles = roleService.selectRolesByUserId(businessIndividual.getUserId());
            Long[] roleIds = roles.stream().filter(SysRole::isFlag).map(SysRole::getRoleId).toArray(Long[]::new);
            // 判断角色列表中是否有商业用户角色（6）
            if (Arrays.stream(roleIds).noneMatch(roleId -> roleId == 6L)) {
                // 添加商业用户角色
                Long[] roleId = new Long[]{6L};
                userService.insertUserAuth(businessIndividual.getUserId(), roleId);
            }
        }
        return businessIndividualMapper.insertBusinessIndividual(businessIndividual);
    }

    /**
     * 修改商业用户信息
     *
     * @param businessIndividual 商业用户信息
     * @return 结果
     */
    @Override
    public int updateBusinessIndividual(BusinessIndividual businessIndividual) {
        // 状态为审核通过时，添加商业角色
        if (businessIndividual.getState() == 2) {
            List<SysRole> roles = roleService.selectRolesByUserId(businessIndividual.getUserId());
            Long[] roleIds = roles.stream().filter(SysRole::isFlag).map(SysRole::getRoleId).toArray(Long[]::new);
            // 判断角色列表中是否有商业用户角色（6）
            if (Arrays.stream(roleIds).noneMatch(roleId -> roleId == 6L)) {
                // 添加商业用户角色
                Long[] roleId = new Long[]{6L};
                userService.insertUserAuth(businessIndividual.getUserId(), roleId);
            }
        }
        return businessIndividualMapper.updateBusinessIndividual(businessIndividual);
    }

    /**
     * 批量删除商业用户信息
     *
     * @param businessIndividualIds 需要删除的商业用户信息主键
     * @return 结果
     */
    @Override
    public int deleteBusinessIndividualByBusinessIndividualIds(Long[] businessIndividualIds) {
        return businessIndividualMapper.deleteBusinessIndividualByBusinessIndividualIds(businessIndividualIds);
    }

    /**
     * 删除商业用户信息信息
     *
     * @param businessIndividualId 商业用户信息主键
     * @return 结果
     */
    @Override
    public int deleteBusinessIndividualByBusinessIndividualId(Long businessIndividualId) {
        return businessIndividualMapper.deleteBusinessIndividualByBusinessIndividualId(businessIndividualId);
    }

    /**
     * 实体数据补全
     *
     * @param businessIndividual 实体
     * @param userId             用户id
     * @param crateType          创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(BusinessIndividual businessIndividual, Long userId, Integer crateType) {
        // 标识
        if (businessIndividual.getBusinessIndividualId() == null || "".equals(businessIndividual.getBusinessIndividualId())) {
            businessIndividual.setBusinessIndividualId(idGeneratorSnowflake.nextId());
        }
        // 创建人、创建时间
        if (crateType == 1) {

            if (businessIndividual.getCreateBy() == null || "".equals(businessIndividual.getCreateBy())) {
                businessIndividual.setCreateBy(userId);
            }
            if (businessIndividual.getCreateTime() == null) {
                businessIndividual.setCreateTime(DateUtils.getNowDate());
            }
        }
        // 用户标识
        if (businessIndividual.getUserId() == null || "".equals(businessIndividual.getUserId())) {
            businessIndividual.setUserId(userId);
        }
        // 逻辑删除
        if (businessIndividual.getIsDeleted() == null || "".equals(businessIndividual.getIsDeleted())) {
            businessIndividual.setIsDeleted(0L);
        }

    }
}
